package basic;

/**
 * @Author: 海琳琦
 * @Date: 2021/8/29 22:31
 */
public class Title859 {
    public static void main(String[] args) {
        System.out.println(buddyStrings("aa","aa"));
    }
    private static boolean buddyStrings(String s, String goal) {
        if(s.length()!=goal.length()){
            return false;
        }
        //字符串相等时判断单字符串是否重复
        if(s.equals(goal)){
            int[] count=new int[26];
            for (int i = 0; i < s.length(); i++) {
                count[s.charAt(i)-'a']++;
                if(count[s.charAt(i)-'a']>1){
                    return true;
                }
            }
            return false;
        }else{
            //当字符串长度相等但内容不相等时，判断（不相等字符出现次数 1 2 3+）
            int a=-1,b=-1;
            for (int i = 0; i < s.length(); i++) {
                if(s.charAt(i)!=goal.charAt(i)){
                    if(a==-1){
                        a=i;
                    }else if(b==-1){
                        b=i;
                    }else{
                        return false;
                    }
                }
            }
            //当不相等字符出现次数为1、2时
            return (b!=-1&&s.charAt(a)==goal.charAt(b)&&s.charAt(b)==goal.charAt(a));
        }
    }
}
